package com.net.picSpider;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Created by buguniao on 16/1/13.
 */
public class Test2 {
    public static void main(String[] args){
        try{
            File imgs = new File("imgs.txt");
            BufferedReader reader = new BufferedReader(new FileReader(imgs));
            String line =null;
            while((line=reader.readLine())!=null){
                String[] arr = line.split("\\|");
                File dir = new File("imgs",arr[0]);
                if(!dir.exists()){
                    dir.mkdirs();
                }
                URL url = new URL(arr[2]);
                download(url,dir);
                System.out.println(line+" 下载成功");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    static void download(URL url,File parent){
        boolean isSuccess=true;
        OutputStream out=null;
        InputStream in=null;
        HttpURLConnection conn=null;
        try{
            conn= (HttpURLConnection)(url.openConnection());
            conn.setConnectTimeout(10000);
            conn.setReadTimeout(10000);

            in= conn.getInputStream();
            int contentLength = conn.getContentLength();
            byte[] buffer = new byte[contentLength];
            int offset=0;

            while(offset<contentLength){
                offset+=in.read(buffer,offset,contentLength-offset);
            }
            int idx = url.getFile().lastIndexOf("/");
            String fileName= url.getFile().substring(idx+1);
            File file = new File(parent,fileName);
            if(!file.exists()){
                file.createNewFile();
            }
            out = new FileOutputStream(file);

            out.write(buffer);

        }catch (Exception e){
            isSuccess=false;
            System.out.println(e.getMessage());
           // e.printStackTrace();
        }finally {
            try{
                if(out!=null){
                    out.close();
                }
                if(in!=null){
                    in.close();
                }
                conn.disconnect();
            }catch (Exception e){
                e.printStackTrace();
            }

        }

        if(!isSuccess){
            download(url,parent);
            System.out.println(url+" 出错，重试");
        }

    }
}
